﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Configuration;
using System.Web.Management;
using SchoolManagerMVC.BusinessConcrete.DTO;
using System.IO;
using System.Data.SqlClient;
using System.Web.Security;
using SchoolManagerMVC.BusinessAbstract.Interfaces;
using SchoolManagerMVC.Repository.EntityFramework;
using SchoolManagerMVC.Web.Models;


namespace SchoolManagerMVC.Web.Areas.Administration.Controllers
{
    public class MembershipController : Controller
    {
        //
        // GET: /Administration/Membership/
        private IRepository _repo;
        private IInitializeBD _initDB;
        public MembershipController(IRepository repo, IInitializeBD initDB)
        {
            _repo = repo;
            if (_repo == null)
                throw new NullReferenceException();

            _initDB = initDB;
            if (_initDB == null)
                throw new NullReferenceException();
        }
        public ActionResult Index()
        {
            return View("CreateAspMembership");
        }


        public ActionResult CreateAspMembership()
        {
            return View("CreateAspMembership");
        }

        [HttpPost]
        public ActionResult CreateAspMembership(AdminCreationDTO adminDTO)
        {
            if (adminDTO.AcceptMemebershipCreation)
            {
                string file="";
                if (Server == null)
                    file = "";
                else
                    file = Server.MapPath(Url.Content("~/SqlScripts/CreateDB.sql"));
                _initDB.createDB(file);

                _initDB.AddMembershipToDB();

                if (Server == null)
                    file = "";
                else
                    file = Server.MapPath(Url.Content("~/SqlScripts/Tables"));
                _initDB.createTables(file);

                if (Server == null)
                    file = "";
                else
                    file = Server.MapPath(Url.Content("~/SqlScripts/Tables/Keys"));
                _initDB.createKeys(file);

                _initDB.createRoles();
                _initDB.addAdminAccount();
                if (adminDTO.LoadTestData)
                {
                    _initDB.addSampleAccounts(_repo);
                    _initDB.addSampleData();
                }
                _initDB.disableDbCreation();

                return View("Success");
            }
            return View("CreateAspMembership");
        }
    }
}

